import Vue from 'vue';
import { ACCESS_TOKEN, USER_TYPE } from '../../store/mutation-types'

const user = {
  state: {
    token: '',// token,
    rememberToken:'',
    username: '',// 用户账户
    realname: '',// 真实姓名
    avatar: '',// 头像地址
    avatar_fileUrl:'',// 头像真实地址
    permissionList: [],// 菜单
    info: {},// 用户信息
    auth:[], // 认证按钮
    type:'',// 用户类型
  },

  mutations: {
    SET_AVATAR_FILE_URL: (state, avatar_fileUrl)=>{
      state.avatar_fileUrl = avatar_fileUrl;
    },
    // 登录成功后保存token
    SET_TOKEN: (state, token) => {
      state.token = token;
      state.rememberToken = token;
      Vue.ls.set(ACCESS_TOKEN, token);
    },

    SET_TYPE: (state, type) => {
      state.type = type;
      Vue.ls.set(USER_TYPE, type)
    },
    SET_NAME: (state, { username, realname }) => {
      state.username = username
      state.realname = realname
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_PERMISSIONLIST: (state, permissionList) => {
      state.permissionList = permissionList
    },
    SET_AUTH:(state, auth)=>{
      state.auth = auth;
    },
    SET_INFO: (state, info) => {
      state.info = info;
      state.username = info.username;
      state.avatar = info.headimg;
      state.avatar_fileUrl = info.headimg_fileUrl;
      state.realname = info.realname;
    },
    CLEAR_USER:(state)=>{
      Vue.ls.remove(ACCESS_TOKEN);
      state.info = {};
      state.username = "";
      state.avatar = "";
      state.avatar_fileUrl = "";
      state.realname = "";
      state.token="";
      state.permissionList=[];
      state.auth=[];
      state.rememberToken=''
    },
  },

  actions: {

  }
};

export default user